package tech.abing.spider.task.pageprocessor.amac;

import java.util.List;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.PageRequest.PostType;
import tech.abing.spider.task.utils.TaskUtil;

public class OutsourceOrganizationProcess implements PageProcessor{

	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		Document doc = Jsoup.parse(page.getContent());
		PageRequest request = page.getRequest();
		
		String footer = doc.select("#body > form > table:nth-child(22) > tbody > tr > td").text();
		Integer currentPage = (Integer)page.getRequest().getExtParams().get("currentPage");
		if(currentPage==null){//由于没有抓取的结束条件，所以用一个变量标记当前抓取的分页
			currentPage = 0;
		}
		int totalPages = TaskUtil.str2Int(footer.substring(footer.indexOf("共")+1, footer.lastIndexOf("页")));
		String GSFWLX = (String) request.getParamsByName("GSFWLX");
		if(totalPages > 1 && currentPage<totalPages){
			for(int i = 2 ;i<=totalPages;i++){
				String url = request.getUrl();
				PageRequest req = context.createPageRequest(url, OutsourceOrganizationProcess.class);
				req.setHeaders(request.getHeaders());
				req.putHeader("Content-Type","application/x-www-form-urlencoded");
				req.putHeader("Origin", "http://fo.amac.org.cn");
				req.putHeader("Referer", "http://fo.amac.org.cn/amac/allNotice.do");
				req.setMethod(Method.POST);
				req.setPostType(PostType.FORMDATA);
				
				
				req.putParams("currentPage", String.valueOf(i));
				req.putParams("mname", "");
				req.putParams("GSJGMC", "");
				req.putParams("GSFWLX", GSFWLX);
				req.putParams("GSID", "");
				req.getExtParams().put("currentPage", i);
				queue.add(req);
			}
		}
		
		
			Elements link = doc.select("a");
			for (Element element : link) {
				String href = element.attr("href");
				if(href.contains("viewNoticeForm")){
					String id = href.substring(href.indexOf(",")+3,href.indexOf("\");"));
					String url = request.getUrl();
					PageRequest req = context.createPageRequest(url, OutsourceOrganizationDetailProcess.class);
					
					req.setMethod(Method.POST);
					req.setPostType(PostType.FORMDATA);
					req.setHeaders(request.getHeaders());

					if(request.getParamsByName("currentPage")!=null){
						req.putParams("currentPage", (String)request.getParamsByName("currentPage"));;
					}else{
						req.putParams("currentPage", "1");
					}
					req.putParams("mname", "viewNoticeForm");
					req.putParams("GSJGMC", "");
					req.putParams("GSFWLX", GSFWLX);
					req.putParams("GSID", id);
					
					queue.add(req);
				}
			}
		
		
		
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
